package com.example.demosecurityjwtlogin.controllers;

import com.example.demosecurityjwtlogin.models.Book;
import com.example.demosecurityjwtlogin.security.services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/book")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/")
    public List<Book> getAllBook() {
        return bookService.getAllBook();
    }

    @PostMapping("/add")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public int addBook(@RequestBody Book book) {
        return bookService.addBook(book);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public int deleteBookById(@PathVariable int id) {
        return bookService.deleteBookById(id);
    }

    @PutMapping("/up")
    @PreAuthorize("hasRole('MODERATOR')")
    public int updateBookById(@RequestBody Book book) {
        return bookService.updateBookById(book);
    }
}

